﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Globalization;

namespace TradeHelper
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            string strAccessConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Base.mdb";
            //string strAccessSelect = "SELECT * FROM Market";

            //DataSet myDataSet = new DataSet();
            myAccessConn = null;

            myAccessConn = new OleDbConnection(strAccessConn);

            //OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn);
            //OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);

           // myAccessConn.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @".\Storage";

            OleDbCommand myCommand;

            string myExecuteQuery;
            myAccessConn.Open();
            myExecuteQuery = "Delete from market";

            myCommand = new OleDbCommand(myExecuteQuery, myAccessConn);
            myCommand.ExecuteNonQuery();

            DirectoryInfo Folder = new DirectoryInfo(path);
            string[] split;

            foreach (FileInfo NextFile in Folder.GetFiles())
            {
                if (NextFile.Extension.ToLower() == ".txt")
                {
                    using (StreamReader sr = File.OpenText(path + @"\" + NextFile.Name))
                    {
                        string s = "";
                        s = sr.ReadLine();
                        this.richTextBox1.AppendText(NextFile.Name+"\n");
                        while ((s = sr.ReadLine()) != null)
                            {                                
                                split = s.Split(',');

                                myExecuteQuery = "INSERT INTO Market (price,           volRemaining,    typeID,          range,           orderID,         volEntered,      minVolume,       bid,           duration, stationID, regionID, solarSystemID, jumps) VALUES" +
                                                                "(" + split[0] + "," + split[1] + "," + split[2] + "," + split[3] + "," + split[4] + "," + split[5] + "," + split[6] + "," + split[7] + "," + split[9] + "," + split[10] + "," + split[11] + "," + split[12] + "," + split[13] + ")";

                                myCommand = new OleDbCommand(myExecuteQuery, myAccessConn);
                                myCommand.ExecuteNonQuery();
                            }                           
                        }                     
                        // path + @"\" +NextFile.Name
                    }
                }

            myAccessConn.Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void btnIGB_Click(object sender, EventArgs e)
        {
            string strAccessConn2 = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Projects\TradeHelper\TradeHelper\bin\Debug\Storage\EVEBase.mdb";
            OleDbConnection myAccessConn2;
            List<string> list = new List<string>();

            StreamWriter sw = new StreamWriter(@"D:\Projects\TradeHelper\TradeHelper\bin\Debug\Storage\index.html");

            StreamReader sr = File.OpenText(@"D:\Projects\TradeHelper\TradeHelper\bin\Debug\Storage\begin.tmpl");
            while (!sr.EndOfStream)
            {
                sw.WriteLine(sr.ReadLine());
            }
            sr.Close();
            using (OleDbConnection connection = new OleDbConnection(strAccessConn2))
            {
                // Create the command and set its properties.
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                command.CommandText = "SELECT typeID, typeName from IGBT2";
                command.CommandType = CommandType.Text;

                // Open the connection and execute the reader.
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                sw.Write(@"var ids = [");

                if (reader.HasRows & reader.Read())
                { 
                    sw.Write(reader[0].ToString());
                }

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        sw.Write(","+reader[0].ToString());
                        list.Add(@"<a href='#' id='item" + reader[0].ToString() + @"' class='normalOrder' onclick='CCPEVE.showMarketDetails(" + reader[0].ToString() + @")'>" + reader[1].ToString() + @"</a><br />");
                    }
                }
                sw.Write(@"];");
                reader.Close();
            }

            sr = File.OpenText(@"D:\Projects\TradeHelper\TradeHelper\bin\Debug\Storage\middle.tmpl");
            while (!sr.EndOfStream)
            {
                sw.WriteLine(sr.ReadLine());
            }
            sr.Close();


            foreach (string str in list)
            {
                sw.WriteLine(str);
            }

            sr = File.OpenText(@"D:\Projects\TradeHelper\TradeHelper\bin\Debug\Storage\end.tmpl");
            while (!sr.EndOfStream)
            {
                sw.WriteLine(sr.ReadLine());
            }
            sr.Close();
            sw.Close();

        }
    }
}
